#!/usr/bin/python
#
# absynth.py
# Copyright (C) Nicola Bizzoca   2006  <hannibal_smith@users.sourceforge.net>
# Copyright (C) Simone Economo   2006  <ekoes@users.sourceforge.net>
# 
# Absynth  is free software.
# 
# You may redistribute it and/or modify it under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 2 of the License, or (at your option)
# any later version.
# 
# main.py is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with main.py.  If not, write to:
#     The Free Software Foundation, Inc.,
#     51 Franklin Street, Fifth Floor
#     Boston, MA  02110-1301, USA.

import os, sys

try:
    import psyco
    psyco.full()
except ImportError:
    print "Absynth supports \"psyco\", an inline optimizer for python.\n" \
	   	  "You can get it at http://sourceforge.net/projects/psyco/."

from abcommon import *
from UInterface import *

class Absynth:
    def __init__(self):
        self.__UIManager = Invoker(UIEngine)
        
		# setting some aliases...
    	self.__widgets = 		self.__UIManager.widgets
    	self.__getWidget = 		self.__widgets.get
    	self.__selectWidget = 	self.__widgets.select
    	self.__connectWidgets =	self.__widgets.connect
    	self.__updateEvents	=	self.__widgets.glade.signal_autoconnect
    	
    	self.loadWidgets()
    	self.__connectWidgets()
         
    def loadWidgets(self):
        	
            def localView():
        		localView = self.__selectWidget["LocalView"].Main(
        				self.__getWidget("LocalTree"), 
        				self.__getWidget("LocalPathText"),
        				mimetypes)
        		localView.setUp()
        		localView.populate()
        		localPath = localView.path
        		
        		self.__updateEvents({
        			"on_LocalTree_row_clicked" : 	localView.onClick,
        			"on_LocalPathText_activate":	localPath.onChange,
        			#"on_LocalPath_changed":		localPath.onChange,
        		})
        		
            def remoteView():
        		connectionWidgets = {
        			"hostname": self.__getWidget("HostnameBox"),
        			"username":	self.__getWidget("UsernameBox"),
        			"password": self.__getWidget("PasswordBox"),
        			"door":		self.__getWidget("DoorBox"),
        			"type":		self.__getWidget("ConnectionTypeBox"),
        			}
        		
        		remoteView = self.__selectWidget["RemoteView"].Main(
        				self.__getWidget("RemoteTree"), 
        				self.__getWidget("RemotePathText"),
        				mimetypes,
        				connectionWidgets,
        				FTPEngine)
        		remoteView.setUp()
        		remoteView.populate()
        		remotePath = remoteView.path
        		
        		self.__updateEvents({
        			"on_RemoteTree_row_clicked" : 	remoteView.onClick,
        			"on_RemotePathText_activate":	remotePath.onChange,
        			#"on_RemotePath_changed":		localPath.onChange,
        		})
        	
            def sysTray():
        		popupMenuEvents = {
        			"on_about" : 	self.__selectWidget["AboutDialog"].Main,
        			"on_destroy" :	gtk.main_quit
        		}
        		sysTray = self.__selectWidget["SysTray"].Main("MenuSysTray", popupMenuEvents)
        		
            def logView():
        		self.logViewWidget = self.__selectWidget["LogView"].Main(self.__getWidget("LogView"))
        		self.logViewWidget.write("Ciao, io sono Absynth! :-)")
        	
        	# Run here your inner functions	
        		localView()
        		remoteView()
        		sysTray()
        		logView()
                
    def start(self):
        self.__UIManager.mainLoop()        
                    
if __name__ == "__main__":
    try:       
    	absynthApp = Absynth()
    	#absynthApp.loadWidgets()
    	absynthApp.start() # YEAH, DIE GFTP!
        
    except Exception, e:
    	errormsg = e.args[0]
    	if errormsg == "'NoneType' object has no attribute 'set_title'":
    		HandleError("Glade","Could not find '%s' in Glade's project." % conf.WINDOW_TYPE).runError()
    	else:
    		HandleError(e.__class__.__name__, errormsg, skipsearch = True).runError()